package com.xcms.test.news;

import Homework.cms.util.Page;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HoutaiServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");     //告诉它当前文档的编码
        response.setHeader("Content-Type","text/html;utf-8");    //告诉浏览器文档类型
        String action=request.getParameter("action");
        PrintWriter out=response.getWriter();   //获取一个打印流，向网页输出打印
//        if("add".equals(action)){
//            String title=request.getParameter("title");
//            News news=new News();
//            news.setTitle(title);
//            if(NewsDao.insert(news)){   //如果添加成功，更新content里面的LatestNews
//                ServletContext context=this.getServletContext();
//                List<News> newsList=NewsDao.getLatestNews();
//                context.setAttribute("LatestNews",newsList);
//            }
//            response.sendRedirect("admin/newsmanager.jsp");
//        }
//        if("page".equals(action)){
//            int currPage=Integer.parseInt(request.getParameter("currPage"));
//            Page<News> page=NewsDao.getPage(10,currPage);
//            request.setAttribute("page",page);
//            request.getRequestDispatcher("about2.jsp").forward(request,response);
//        }
        if("delete".equals(action)){
            String id=request.getParameter("id");
            News news=new News();
            news.setId(Integer.parseInt(id));
            NewsDao.remove(news);
            out.println("删除成功！");
            response.setHeader("Refresh","3;url=http://localhost:8080/webtest/Homework/manage/news?action=page2&currPage=1");
        }

        if("page2".equals(action)){
            int currPage=Integer.parseInt(request.getParameter("currPage"));
            Page<News> page2=NewsDao.getPage(10,currPage);
            request.setAttribute("page2",page2);
            request.getRequestDispatcher("design.jsp").forward(request,response);
        }


    }
}
